﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Core.BaseCore.Extensions;
using Chinaer.Core.Helper;
using Chinaer.Model.Common;

namespace Chinaer.Bll
{
    public class Administrator : Repository, IBll.IAdministrator
    {
        IDao.IAdministratorDAO administratorDAO = new Dao.AdministratorDAO();
        public Model.Common.JsonResult Add(DTO.AdministratorDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            if (!string.IsNullOrEmpty(t.LoginName))
            {
                if (administratorDAO.GetByName(t.LoginName) != null)
                {
                    jsonResult.Success = false;
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                    jsonResult.ErrorMessage = "该用户已经存在";
                    return jsonResult;
                }
            }
            try
            {
                Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Administrator);
                if (model_Administrator.ID.CheckIsNull().Success == false)
                {
                    model_Administrator.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
                }
                jsonResult.Success = administratorDAO.Add(model_Administrator);
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(DTO.AdministratorDTO t)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                if (t.CheckIsNull().Success == false)
                {
                    return t.CheckIsNull();
                }
                Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Administrator);
                jsonResult.Success = administratorDAO.Delete(model_Administrator);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Delete(string id)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            try
            {
                if (id.CheckIsNull().Success == false)
                {
                    return id.CheckIsNull();
                }
                jsonResult.Success = administratorDAO.Delete(id);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public Model.Common.JsonResult Update(DTO.AdministratorDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();

            try
            {
                Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Administrator);
                jsonResult.Success = administratorDAO.Update(model_Administrator);
            }
            catch (Exception ex)
            {
                jsonResult = ex.ExceptionJsonResult();
            }
            return jsonResult;
        }

        public DTO.AdministratorDTO GetByID(string id)
        {
            DTO.AdministratorDTO administratorDTO = new DTO.AdministratorDTO();

            try
            {
                if (id.CheckIsNull().Success == false)
                {
                    return null;
                }
                Model.Entities.Model_Administrator model_Administrator = new Model.Entities.Model_Administrator();
                model_Administrator = administratorDAO.GetByID(id);
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Administrator, administratorDTO);
            }
            catch (Exception ex)
            {
                administratorDTO = null;
            }
            return administratorDTO;
        }

        public List<DTO.AdministratorDTO> GetAllAdministrators()
        {
            List<DTO.AdministratorDTO> listAdministrator = new List<DTO.AdministratorDTO>();
            List<Model.Entities.Model_Administrator> listModelAdministrator = new List<Model.Entities.Model_Administrator>();
            try
            {
                listModelAdministrator = administratorDAO.GetAllAdministrators();
                if (listModelAdministrator != null && listModelAdministrator.Count > 0)
                {
                    foreach (var item in listModelAdministrator)
                    {
                        DTO.AdministratorDTO administratorDTO = new DTO.AdministratorDTO();
                        Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, administratorDTO);
                        listAdministrator.Add(administratorDTO);                    
                    }
                }
            }
            catch (Exception ex)
            {
                
                listAdministrator = null;
            }
            return listAdministrator;
        }
    }
}
